<?php
// questo script aggiunge nella tabella manufacturer_to_store
// le marche inserite non presenti
 
// Configuration
require_once('../config.php');

define ('DIR_IMAGE_MANUFACTURER', DIR_IMAGE .'data/manufacturer/' );
define ('DIR_IMAGE_LOAD', DIR_SYSTEM . '../var/caricamento/image/' );

$file_log = '';

if ( count($argv) ) {
	$file_log = $argv[1];
}

define ('FILE_LOG',   $file_log);

// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Config
$config = new Config();

// Database
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

require_once('common.php');

$queryStr = "select * from manufacturer_load where elab_chk = 0";

$manufacturer_load = $db->query($queryStr);

if ($manufacturer_load->num_rows) {
	
  LogBatch::getInstance()->write('Ci sono marche '. $manufacturer_load->num_rows .' caricate sulla manufacturer_load');

	foreach ($manufacturer_load->rows as $manufacturer) {
	
		$manufacturer_load_id = $manufacturer['manufacturer_load_id'];
		$name                 = trim($manufacturer['name']);
		$image                = trim($manufacturer['image']);
    $manufacturer_code    = trim($manufacturer['manufacturer_code']);

    $name               = $db->escape($name);
    $manufacturer_code  = $db->escape($manufacturer_code);

    // verifico se si tratta di un inserimento o aggiornamento
	  $queryStr = "select manufacturer_id from manufacturer where manufacturer_code = '$manufacturer_code'";
    $query = $db->query($queryStr);

    $manufacturer_id = -1;
    $esito = 'Inserito nuovo record';
    
    if ( $query->num_rows ) {
      // si tratta di un aggiornamento    	
      LogBatch::getInstance()->write('Aggiorno la marca con manufacturer_code ='. $manufacturer_code);
      
      $manufacturer_id = $query->row['manufacturer_id'];
       
      $queryStr = "update manufacturer set name = '$name' where manufacturer_id = $manufacturer_id";
      $db->query($queryStr);
      
      $esito = 'Aggiornato record';
    } else {
	    // si tratta di un nuovo inserimento
	    LogBatch::getInstance()->write('Inserisco la nuova marca con manufacturer_code ='. $manufacturer_code);
	
	    // inserisco la categoria
	    $queryStr = " insert into manufacturer (name, manufacturer_code)
	                  values  ('$name', '$manufacturer_code')";
	    $db->query($queryStr);
	    $manufacturer_id = $db->getLastId();
	
	    $queryStr = "insert into manufacturer_to_store values ( $manufacturer_id, 0 )"; 
	    $db->query($queryStr);  
    }    

    // Aggiungo la gestione della image
    if ( $image != '' ) { // se il nome dell'image e' valorizzato

      $imageNew = $image;
           
      // LogBatch::getInstance()->write('Image = ['. $image.']');

      if ( file_exists ( DIR_IMAGE_LOAD . $image ) ) {
          
        LogBatch::getInstance()->write('L\'image = ['. $image. '] della marca esiste nella directory di caricamento');

        // image esiste nella directory caricamento ... verifico se esiste
        // nel sistema un'image con lo stesso nome ...
        if ( file_exists ( DIR_IMAGE_MANUFACTURER . $image ) ) {
          LogBatch::getInstance()->write('L\'image = ['. $image. '] esiste nella directory di '.DIR_IMAGE_MANUFACTURER.' la rinomino ...');
          $imageNew = $manufacturer_code.'_'.$image; // aggiungo il codice categoria
        }

        // LogBatch::getInstance()->write('Image ='. $image);
      }

      // copiare l'image
      if (!@copy(DIR_IMAGE_LOAD . $image, DIR_IMAGE_MANUFACTURER . $imageNew)) {
      	LogBatch::getInstance()->write('Fallito copia della image ='. $image. ' nella cartella di sistema');
      } else {
	      $image = 'data/manufacturer/'. $imageNew;        
	      $image    = $db->escape($image);
	      $queryStr = "UPDATE manufacturer set image = '$image' where manufacturer_id = $manufacturer_id";
	      $db->query($queryStr);	
      }
    } // end if ( $image != '' )
    
    $db->query("update manufacturer_load set elab_chk = 1, esito = '$esito' where manufacturer_load_id = $manufacturer_load_id ");
	}

	$db->query("optimize table manufacturer");
	$db->query("optimize table manufacturer_load");
	
} else {
  LogBatch::getInstance()->write('Non ci sono marche caricate sulla manufacturer_load');
}

LogBatch::getInstance()->write('Fine caricamento.');
